package com.windward.vii.service.tupu;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;

import net.sf.json.JSONObject;

import com.windward.vii.service.tupu.model.Options;
import com.windward.vii.service.tupu.util.ConfigUtil;

public class TupuApiTest {

	public static void main(String[] args) {
		// secret id
		String secretId = "59eedf4ce61df55e6e051d12";
		// private KEY path
		String privateKeyPath = "pkcs8_private_key.pem";
		// request Url
		String requestUrl = "http://api.open.tuputech.com/v3/recognition/";
		// fileList imageFile or url
		ArrayList<String> fileLists = new ArrayList<String>();
		fileLists.add("https://vii-img.oss-cn-beijing.aliyuncs.com/user_3947857_photo_20171024044752910-4720_0.png?x-oss-process=image/resize,m_fill,w_100,h_100,limit_0/auto-orient,1/quality,q_90");

		// tags
		String tags[] = { "1"};
		// options
		Options options = new Options();
		options.setTags(tags);
		// options.setUid("your_uid"); // 第三方客户标识

		TupuApiTest test = new TupuApiTest();
		String filePath = test.getFile(privateKeyPath);
		System.out.println("filePath="+filePath);
		TupuApi tupuApi = new TupuApi(secretId, filePath, requestUrl);
		JSONObject result = tupuApi.doApiTest(ConfigUtil.UPLOAD_TYPE.UPLOAD_URI_TYPE, fileLists, options);

		System.out.println(result);
	}

	private String getFile(String fileName) {
		ClassLoader classLoader = getClass().getClassLoader();
		/**
		 getResource()方法会去classpath下找这个文件，获取到url resource, 得到这个资源后，调用url.getFile获取到 文件 的绝对路径
		 */
		URL url = classLoader.getResource(fileName);
		/**
		 * url.getFile() 得到这个文件的绝对路径
		 */
		System.out.println(url.getFile());
		File file = new File(url.getFile());
		System.out.println(file.exists());

		return url.getPath();
	}

}
